<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="lib/bootstrap.css">
    <script src="lib/jquery.js"></script>
</head>

<body style="padding: 20px;">
    <!-- 添加图书的Pannel面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加新图书</h3>
        </div>
        <div class="panel-body form-inline">
            <div class="input-group">
                <div class="input-group">
                    <div class="input-group-addon">书名</div>
                    <input type="text" class="form-control bookName" id="exampleInputAmount" placeholder="请输入书名">
                </div>
                <div class="input-group">
                    <div class="input-group-addon">作者</div>
                    <input type="text" class="form-control author" id="exampleInputAmount" placeholder="请输入作者">
                </div>
                <div class="input-group">
                    <div class="input-group-addon">出版社</div>
                    <input type="text" class="form-control publisher" id="exampleInputAmount" placeholder="请输入出版社">
                </div>
                <button type="button" class="btn btn-primary">添加</button>
            </div>

        </div>
    </div>
    <!-- 图书表格 -->

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <!-- <tr>
                <td></td>
            </tr> -->
        </tbody>
    </table>

    <script>
        $(function () {
            // 渲染页面函数
            function getBookList() {
                $.ajax({
                    method: "get",
                    url: "http://www.liulongbin.top:3006/api/getbooks",
                    success: (res) => {
                        var arr = [];
                        res.data.map((v) => {
                            arr.push(`<tr><td>${v.id}</td><td>${v.bookname}</td><td>${v.author}</td><td>${v.publisher}</td><td><a href="javascript:;" class="del" data-id="${v.id}">删除</a></td></tr>`)
                        })
                        $("tbody").empty().html(arr.join(','))
                    }
                })
            }
            getBookList()
            // 事件代理（事件委派）
            $("tbody").on("click", ".del", function () {
                var index = $(this).attr("data-id")
                $.ajax({
                    method: "get",
                    url: "http://www.liulongbin.top:3006/api/delbook",
                    data: { id: index },
                    success: function (res) {
                        if (res.status !== 200) return alert('删除图书失败！')
                        getBookList()
                    },
                    error: () => alert("删除图书失败！"),
                })
            })

            // 添加图书
            $(".btn").on("click", function () {
                var bookNames = $(".bookName").val().trim();
                var authors = $(".author").val().trim();
                var publishers = $(".publisher").val().trim();
                if (!bookNames || !authors || !publishers) {
                    alert("请输入完整的查询内容！");
                } else {
                    $.ajax({
                        method: "post",
                        url: "http://www.liulongbin.top:3006/api/addbook",
                        data: {
                            bookname: bookNames,
                            author: authors,
                            publisher: publishers
                        },
                        success: (res) => {
                            if (res.status !== 201) return alert('添加图书失败');
                            getBookList();
                        },
                        error: () => alert("添加图书失败"),
                    })
                    $('.bookName').val('')
                    $('.author').val('')
                    $('.publisher').val('')


                }
            })


        })
    </script>
</body>

</html>